﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Crucible.Models;
using WebMatrix.WebData;

namespace Crucible.Controllers
{
    public class NodeVoteController : ApiController
    {
        private ArgumentConnection db = new ArgumentConnection();

        //// GET api/QuestionVote
        //public IEnumerable<NodeVote> GetNodeVotes()
        //{
        //    var assertionvotes = db.AssertionVotes.Include(n => n.Author).Include(n => n.Node);
        //    return assertionvotes.AsEnumerable();
        //}

        //// GET api/QuestionVote/5
        //public NodeVote GetNodeVote(int id)
        //{
        //    NodeVote nodevote = db.AssertionVotes.Find(id);
        //    if (nodevote == null)
        //    {
        //        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        //    }

        //    return nodevote;
        //}

        // PUT api/NodeVote/5
        public HttpResponseMessage PutNodeVote(NodeVote nodevote)
        {

            var userId = WebSecurity.CurrentUserId;

            if (userId != nodevote.Author_UserId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            if (db.AssertionVotes.Any(nv => nv.Author_UserId == userId && nv.Node_Id == nodevote.Node_Id))
            {
                db.Entry(nodevote).State = EntityState.Modified;
            }
            else
            {
                db.AssertionVotes.Add(nodevote);
            }

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, "Concurrency exception.");
            }

            return Request.CreateResponse(HttpStatusCode.OK);
            
        }

        //// POST api/QuestionVote
        //public HttpResponseMessage PostNodeVote(NodeVote nodevote)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.AssertionVotes.Add(nodevote);
        //        db.SaveChanges();

        //        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, nodevote);
        //        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = nodevote.Node_Id }));
        //        return response;
        //    }
        //    else
        //    {
        //        return Request.CreateResponse(HttpStatusCode.BadRequest);
        //    }
        //}

        //// DELETE api/QuestionVote/5
        //public HttpResponseMessage DeleteNodeVote(int id)
        //{
        //    NodeVote nodevote = db.AssertionVotes.Find(id);
        //    if (nodevote == null)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.NotFound);
        //    }

        //    db.AssertionVotes.Remove(nodevote);

        //    try
        //    {
        //        db.SaveChanges();
        //    }
        //    catch (DbUpdateConcurrencyException)
        //    {
        //        return Request.CreateResponse(HttpStatusCode.NotFound);
        //    }

        //    return Request.CreateResponse(HttpStatusCode.OK, nodevote);
        //}

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}